package com.gorillalogic.scrummonkey.dms {
	import com.adobe.fiber.services.wrapper.DataServiceWrapper;
	
	import mx.data.DataService;
	import mx.rpc.AsyncToken;

	public class DmsUtils {
		public static function doTxn(wrapper:DataServiceWrapper, txn:Function):AsyncToken {
			var service:DataService = wrapper.serviceControl;
			if (service.commitRequired) {
				throw new Error("Cannot wait for transaction. A transaction is already open.");
			}

			var prevAutoCommit:Boolean = false;
			if (service.autoCommit) {
				prevAutoCommit = true;
				service.autoCommit = false;
			}

			txn();

			var token:AsyncToken = service.commit();
			service.autoCommit = prevAutoCommit;
			return token;
		}

		public static function getEntityName(entity:Object, pluralize:Boolean = false):String {
			if (entity is Project) {
				return (pluralize ? 'Projects' : 'Project');
			} else if (entity is Release) {
				return (pluralize ? 'Releases' : 'Release');
			} else if (entity is Iteration) {
				return (pluralize ? 'Iterations' : 'Iteration');
			} else if (entity is Objective) {
				return (pluralize ? 'Objectives' : 'Objective');
			} else {
				return (pluralize ? 'unknowns' : 'unknown');
			}
		}
	}
}